/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Interface;

import Connects.Connect;
import Packets.pkValue;
import RemoteDesktop.ClientHook;
import RemoteDesktop.ConnectRemote;
import Shell.ExecuteShell;

import StaticClass.ArrSocket;
import Thread.ConnectFile;
import Thread.ConnectFile_Tree;
import Thread.ConnectImages;
import Thread.ConnectName;
import Thread.ConnectShellSV;
import Thread.Connection;
import Thread.ThreadChat;
import Thread.ThreadCon;
import Thread.ThreadFile;
import Thread.ThreadFile_Tree;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.DataInputStream;
import java.io.IOException;
import java.net.Socket;
import java.util.Vector;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JButton;
import javax.swing.JEditorPane;
import javax.swing.JLabel;
import javax.swing.JLayeredPane;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JScrollPane;
import javax.swing.JSplitPane;
import javax.swing.JTabbedPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author Oho
 */
public class IMain extends javax.swing.JFrame {

    /**
     * Creates new form IMain
     */
    ThreadChat tm;
//    Connection cn;

    public IMain() {
        try {
            //data.read      
            initComponents();
        //   jScrollPane1.setViewportView(scrImages);
//CreatePanel();
            ArrSocket.im = this;
            ConnectName cnn=new ConnectName(2222,jTable1,scrPN);
            cnn.start();
        } catch (IOException ex) {
            Logger.getLogger(IMain.class.getName()).log(Level.SEVERE, null, ex);
        }
        ArrSocket.im = this;
        //this.scrPN.setBounds(0, 0, 2000, 2000);
        this.scrPN.setPreferredSize(new Dimension(2000, 2000));
       
       // scrImages= new JScrollPane(this.scrPN, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
       // this.getContentPane().add(jScrollPane1);

    }

    //JLayeredPane lpane = new JLayeredPane();
    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jToolBar1 = new javax.swing.JToolBar();
        cmdShutdown = new javax.swing.JButton();
        cmdRestart = new javax.swing.JButton();
        cmdLogoff = new javax.swing.JButton();
        btnChat = new javax.swing.JButton();
        btnSendFile = new javax.swing.JButton();
        btnRemoteDT = new javax.swing.JButton();
        btnCMD = new javax.swing.JButton();
        jButton1 = new javax.swing.JButton();
        tab = new javax.swing.JTabbedPane();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTable1 = new javax.swing.JTable();
        scrImages = new javax.swing.JScrollPane();
        scrPN = new javax.swing.JPanel();
        jMenuBar1 = new javax.swing.JMenuBar();
        jMenu1 = new javax.swing.JMenu();
        jMenu2 = new javax.swing.JMenu();
        jMenu3 = new javax.swing.JMenu();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setLocationByPlatform(true);

        jToolBar1.setRollover(true);

        cmdShutdown.setText("Shutdown");
        cmdShutdown.setFocusable(false);
        cmdShutdown.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        cmdShutdown.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        cmdShutdown.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cmdShutdownActionPerformed(evt);
            }
        });
        jToolBar1.add(cmdShutdown);

        cmdRestart.setText("Restart");
        cmdRestart.setFocusable(false);
        cmdRestart.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        cmdRestart.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        cmdRestart.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cmdRestartActionPerformed(evt);
            }
        });
        jToolBar1.add(cmdRestart);

        cmdLogoff.setText("Logoff");
        cmdLogoff.setFocusable(false);
        cmdLogoff.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        cmdLogoff.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        cmdLogoff.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cmdLogoffActionPerformed(evt);
            }
        });
        jToolBar1.add(cmdLogoff);

        btnChat.setText("Chat");
        btnChat.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnChatActionPerformed(evt);
            }
        });
        jToolBar1.add(btnChat);

        btnSendFile.setText("SendFile");
        btnSendFile.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnSendFileActionPerformed(evt);
            }
        });
        jToolBar1.add(btnSendFile);

        btnRemoteDT.setText("RemoteDesktop");
        btnRemoteDT.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnRemoteDTActionPerformed(evt);
            }
        });
        jToolBar1.add(btnRemoteDT);

        btnCMD.setText("CMD");
        btnCMD.setFocusable(false);
        btnCMD.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        btnCMD.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        btnCMD.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCMDActionPerformed(evt);
            }
        });
        jToolBar1.add(btnCMD);

        jButton1.setText("SendMssage");
        jButton1.setFocusable(false);
        jButton1.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        jButton1.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });
        jToolBar1.add(jButton1);

        tab.setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));

        jTable1.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        jTable1.addAncestorListener(new javax.swing.event.AncestorListener() {
            public void ancestorMoved(javax.swing.event.AncestorEvent evt) {
                jTable1AncestorMoved(evt);
            }
            public void ancestorAdded(javax.swing.event.AncestorEvent evt) {
            }
            public void ancestorRemoved(javax.swing.event.AncestorEvent evt) {
            }
        });
        jScrollPane1.setViewportView(jTable1);

        tab.addTab("tab3", jScrollPane1);

        scrImages.setAutoscrolls(true);
        scrImages.setInheritsPopupMenu(true);

        javax.swing.GroupLayout scrPNLayout = new javax.swing.GroupLayout(scrPN);
        scrPN.setLayout(scrPNLayout);
        scrPNLayout.setHorizontalGroup(
            scrPNLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 918, Short.MAX_VALUE)
        );
        scrPNLayout.setVerticalGroup(
            scrPNLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 692, Short.MAX_VALUE)
        );

        scrImages.setViewportView(scrPN);

        tab.addTab("tab2", scrImages);

        jMenu1.setText("File");
        jMenuBar1.add(jMenu1);

        jMenu2.setText("Edit");
        jMenuBar1.add(jMenu2);

        jMenu3.setText("Help");
        jMenuBar1.add(jMenu3);

        setJMenuBar(jMenuBar1);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jToolBar1, javax.swing.GroupLayout.DEFAULT_SIZE, 430, Short.MAX_VALUE)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(tab, javax.swing.GroupLayout.PREFERRED_SIZE, 0, Short.MAX_VALUE)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jToolBar1, javax.swing.GroupLayout.PREFERRED_SIZE, 47, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(tab, javax.swing.GroupLayout.PREFERRED_SIZE, 722, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents
    Socket sk;
    private void btnChatActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnChatActionPerformed
        try {
            // TODO add your handling code here:
            int i = 0;
            if (tab.getSelectedIndex() == 1) {
                //System.out.println(ArrSocket.im.jTable1.getSelectedRowCount());
                //chon lua socket ket noi ! 
                //lay socket ra 
                //thread chat se hoat dong goi ham write 
                i = ArrSocket.im.jTable1.getSelectedRow();
                //sk= ArrSocket.sockList.get(i);

            } else {
                i = ArrSocket.icl.getChiso();

            }
            ThreadChat t = ArrSocket.ArrChat.get(i);
            //JOptionPane.showConfirmDialog(jButton1, jTable1.getSelectedRowCount());
            //tm.setSk(sk);

            ArrSocket.CHAT = "CHAT";
            //tm = new ThreadMain(4567);
            //  ArrSocket.TheadList.add(tm);
            // IChat ic = new IChat(sk,tm);      
            // ArrSocket.ic.add(ic);
            //   System.out.println(i);/*
            IChat ic = new IChat(t);
            ic.setVisible(true);
            ic.pack();
            ArrSocket.ic.add(ic);
            pkValue pk = new pkValue(1, 0, "CHAT");
            t.ThreadWrite(pk);
            //t.start();
            //ic.pack();
            //ic.setVisible(true);
            //  tm.ThreadWrite(pk);
            //tm.start();*/
        } catch (IOException ex) {
            Logger.getLogger(IMain.class.getName()).log(Level.SEVERE, null, ex);

        }
        // */


    }//GEN-LAST:event_btnChatActionPerformed

    private void btnSendFileActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnSendFileActionPerformed
        //try {
        // TODO add your handling code here:
        int i = 0;
        if (tab.getSelectedIndex() == 1) {
            i = ArrSocket.im.jTable1.getSelectedRow();
        } else {
            i = i = ArrSocket.icl.getChiso();
        }
        //sk= ArrSocket.sockList.get(i);
        ThreadFile t = ArrSocket.ArrFile.get(i);
        //JOptionPane.showConfirmDialog(jButton1, jTable1.getSelectedRowCount());
        //tm.setSk(sk);

        ThreadFile_Tree ft = ArrSocket.ArrFileTree.get(i);
        int flag = 0;
        for (IFile ifi : ArrSocket.ArrIFile) {
            if (ifi.tf == t) {
                ifi.setVisible(true);
                flag = 1;
            }
        }
        if (flag == 0) {
            IFile tf = new IFile(t, ft);


            tf.setVisible(true);
        }


        //   ArrSocket.ArrIFile.get(i).setVisible(true);


        //ArrSocket.ArrIFile.get(i).pack();

    }//GEN-LAST:event_btnSendFileActionPerformed

    private void cmdShutdownActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cmdShutdownActionPerformed
        // TODO add your handling code here:
        int i = 0;
        if (tab.getSelectedIndex() == 1) {
            i = ArrSocket.im.jTable1.getSelectedRow();
        } else {
            i = i = ArrSocket.icl.getChiso();
        }
        //sk= ArrSocket.sockList.get(i);
        ExecuteShell t = ArrSocket.ArrShell.get(i);
        t.WriteExecute("shutdown /s", null);
        //t.WriteExecute("shut down");

    }//GEN-LAST:event_cmdShutdownActionPerformed

    private void cmdRestartActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cmdRestartActionPerformed
        // TODO add your handling code here:
        int i = 0;
        if (tab.getSelectedIndex() == 1) {
            i = ArrSocket.im.jTable1.getSelectedRow();
        } else {
            i = i = ArrSocket.icl.getChiso();
        }
        //sk= ArrSocket.sockList.get(i);
        ExecuteShell t = ArrSocket.ArrShell.get(i);
        //  ClientHook  t = ArrSocket.ArrHook.get(i);
        t.WriteExecute("shutdown /r", null);
        //t.WriteExecute("restart");
    }//GEN-LAST:event_cmdRestartActionPerformed

    private void cmdLogoffActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cmdLogoffActionPerformed
        // TODO add your handling code here:
        int i = 0;
        if (tab.getSelectedIndex() == 1) {
            i = ArrSocket.im.jTable1.getSelectedRow();
        } else {
            i = i = ArrSocket.icl.getChiso();
        }
        //sk= ArrSocket.sockList.get(i);
        ExecuteShell t = ArrSocket.ArrShell.get(i);
        t.WriteExecute("shutdown /l", null);
    }//GEN-LAST:event_cmdLogoffActionPerformed

    private void btnRemoteDTActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnRemoteDTActionPerformed
        // TODO add your handling code here
        int i = 0;
        if (tab.getSelectedIndex() == 1) {
            i = ArrSocket.im.jTable1.getSelectedRow();
        } else {
            i = i = ArrSocket.icl.getChiso();
        }
        //sk= ArrSocket.sockList.get(i);
        ClientHook t = ArrSocket.ArrHook.get(i);
        t.start();
    }//GEN-LAST:event_btnRemoteDTActionPerformed

    private void btnCMDActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnCMDActionPerformed
        // TODO add your handling code here:
        int i = 0;
        if (tab.getSelectedIndex() == 1) {
            i = ArrSocket.im.jTable1.getSelectedRow();
        } else {
            i = i = ArrSocket.icl.getChiso();
        }
        ExecuteShell ex = ArrSocket.ArrShell.get(i);
        CMD frm = new CMD(ex);
        frm.setVisible(true);
    }//GEN-LAST:event_btnCMDActionPerformed

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        // TODO add your handling code here:
        SendMessage sd = new SendMessage();
        sd.setVisible(true);
    }//GEN-LAST:event_jButton1ActionPerformed

    private void jTable1AncestorMoved(javax.swing.event.AncestorEvent evt) {//GEN-FIRST:event_jTable1AncestorMoved
        // TODO add your handling code here:
    }//GEN-LAST:event_jTable1AncestorMoved

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(IMain.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(IMain.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(IMain.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(IMain.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new IMain().setVisible(true);

            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnCMD;
    private javax.swing.JButton btnChat;
    private javax.swing.JButton btnRemoteDT;
    private javax.swing.JButton btnSendFile;
    private javax.swing.JButton cmdLogoff;
    private javax.swing.JButton cmdRestart;
    private javax.swing.JButton cmdShutdown;
    private javax.swing.JButton jButton1;
    private javax.swing.JMenu jMenu1;
    private javax.swing.JMenu jMenu2;
    private javax.swing.JMenu jMenu3;
    private javax.swing.JMenuBar jMenuBar1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable jTable1;
    private javax.swing.JToolBar jToolBar1;
    private javax.swing.JScrollPane scrImages;
    private javax.swing.JPanel scrPN;
    private javax.swing.JTabbedPane tab;
    // End of variables declaration//GEN-END:variables
}
